package com.dede.state.component;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.View;

import com.dede.state.StateLayout;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * 状态组件
 *
 * @author hsh
 *         2018/1/22 17:54.
 */
public abstract class StateComponent implements Serializable {

    private Context context;
    private View rootView;
    private ReloadListener reloadListener;
    private boolean showSuccess;

    @LayoutRes
    protected abstract int getLayoutId();

    /**
     * 是否回调ReloadListener
     * false 调用默认的ReloadListener.reload方法
     * true 不调用ReloadListener.reload方法
     *
     * @return 默认false 调用
     */
    protected boolean onClickEvent() {
        return false;
    }

    public StateComponent() {
    }

    public StateComponent setParams(Context context, @Nullable ReloadListener reloadListener) {
        this.context = context;
        this.reloadListener = reloadListener;
        return this;
    }

    public void setConentView(View view) {
        if (view == null) return;
        this.rootView = view;
    }

    /**
     * SuccessComponent会调用当前构造方法
     * @param rootView
     * @param context
     * @param reloadListener
     */
    StateComponent(View rootView, Context context, @Nullable ReloadListener reloadListener) {
        this.rootView = rootView;
        this.context = context;
        this.reloadListener = reloadListener;
    }

    /**
     * 是否显示成功页面
     *
     * @return
     */
    public boolean isShowSuccess() {
        return showSuccess;
    }

    /**
     * 设置是否显示成功页面
     *
     * @param showSuccess
     */
    protected void setShowSuccess(boolean showSuccess) {
        this.showSuccess = showSuccess;
    }

    public View getRootView() {
        int layoutId = getLayoutId();
        if (layoutId == 0 && rootView != null) {
            return rootView;
        }

        if (rootView == null) {
            rootView = View.inflate(context, layoutId, null);
        }

        if (rootView == null) {
            throw new IllegalArgumentException("RootView is empty " + this.getClass());
        }

        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean click = onClickEvent();
                if (click) {
                    return;
                }
                if (StateComponent.this.reloadListener != null) {
                    StateComponent.this.reloadListener.reload(StateComponent.this.getClass());
                }
            }
        });

        onViewCreate(rootView);
        return rootView;
    }

    /**
     * onViewCreate
     *
     * @param view
     */
    protected void onViewCreate(View view) {
    }

    /**
     * 重新加载回调
     */
    public interface ReloadListener extends Serializable {

        void reload(Class<? extends StateComponent> clazz);
    }

    /**
     * 每一个StateComponent都有可能被多次add，所以在{@link StateLayout#addStateComponent(StateComponent)}方法内复制当前对象
     *
     * @return copy
     */
    public StateComponent copy() {
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        ObjectOutputStream oos;
        Object obj = null;
        try {
            oos = new ObjectOutputStream(bao);
            oos.writeObject(this);
            oos.close();
            ByteArrayInputStream bis = new ByteArrayInputStream(bao.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            obj = ois.readObject();
            ois.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (StateComponent) obj;
    }

}
